// pages/chat/chat.js
var socketTask;
import config from '../../utils/config'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sendMessage: '',
        senderId: '',
        receiverId: '',
        senderAvator: '',
        receiverAvator: '',
        receiverNickname: '',
        senderNickname: '',
        MessageList: [],
        scrollTop: 0 // 内容底部与顶部的距离
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            receiverId: options.id,
            receiverAvator: options.avator,
            receiverNickname: options.nickName
        })
        wx.setNavigationBarTitle({
            title: options.nickName,
        })
        const that = this
        wx.getStorage({
            "key": "userInfo",
            success: (res) => {
                
                that.setData({
                    senderId: res.data.userId,
                    senderAvator: res.data.avator,
                    senderNickname: res.data.nickName
                })
            }
        })
        socketTask = wx.connectSocket({
            url: 'ws://localhost:4000/chat',
        })
        socketTask.onOpen((result) => {
            console.log('连接成功！');
            this.getHistoryList()
            const initData = JSON.stringify({
                "id": this.data.senderId,
                "login": true,
            });
            socketTask.send({
                data: initData
            });
        })
        socketTask.onError(function (res) {
            console.log('WebSocket连接打开失败，请检查！')
        })
        socketTask.onMessage(function (res) {
            const data = JSON.parse(res.data)
            const list = that.data.MessageList
            list.push({
                id: data.data.id,
                msg: data.data.msg,
            })
            that.setData({
                MessageList: list
            })

        })
    },
    sendMessage() {
        if (this.data.sendMessage == '') {
            return;
        }
        let that = this
        const data = JSON.stringify({
            "id": this.data.senderId, // 当前ID
            "login": false, // 是否登陆
            "to": this.data.receiverId, // 发送对象
            "msg": this.data.sendMessage // 发送内容
        })
        socketTask.send({
            data: data,
            success(res) {
                let list = that.data.MessageList;
                list.push({
                    "id": that.data.senderId, // 当前ID
                    "msg": that.data.sendMessage, // 发送内容  
                })
                that.setData({
                    sendMessage: '',
                    MessageList: list
                })

            }
        })
    },
    getMessage(e) {
        this.setData({
            sendMessage: e.detail.value
        })

    },
    getHistoryList() {
        let data = {
            senderId: this.data.senderId,
            receiverId: this.data.receiverId
        }
        let that=this
        wx.request({
            url: `${config.baseUrl}/getHistoryList`,
            data: data,
            method: "GET",
            success: (res) => {
                let list = that.data.MessageList;
                list=list.concat(res.data.data)
                that.setData({
                    MessageList:list
                })
            }
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})